﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using GTA;

namespace GTAHookAdv.Overlay
{
    /// <summary>
    /// This class warp <see cref="System.Windows.Forms.PictureBox"/> to GTANetScriptHook.
    /// </summary>
    public class PictureBoxOverlay : ControlOverlay<System.Windows.Forms.PictureBox>
    {

        private g_Texture _texture;

        /// <summary>
        /// see <see cref="ControlOverlay{T}(GTA.Script)">ControlOverlay{T} constructor</see> for more details
        /// </summary>
       
        public PictureBoxOverlay(Script owner)
            : base(owner)
        {
            base.ForeColor = Color.White;
            Ctrl.SizeChanged += Ctrl_SizeChanged;
            base.OpacityChanged += PictureBoxOverlay_OpacityChanged;
            base.LocationChanged += PictureBoxOverlay_LocationChanged;
            base.ForeColorChanged += PictureBoxOverlay_ForeColorChanged;
        }

        void PictureBoxOverlay_ForeColorChanged(object sender, EventArgs e)
        {
            if (_texture != null)
                _texture.Color = base.ForeColor;
        }

        void PictureBoxOverlay_LocationChanged(object sender, EventArgs e)
        {
            if (_texture != null)
            {
                _texture.Location = base.Location;
                _texture.LocationOffset = PointToScreen(new Point(0, 0));
            }
        }

        void PictureBoxOverlay_OpacityChanged(object sender, EventArgs e)
        {
            if (_texture != null)
                _texture.Opacity = base.Opacity;
        }

        void Ctrl_SizeChanged(object sender, EventArgs e)
        {
            if (_texture != null)
                _texture.SizeToDisplay = Ctrl.Size;
        }

#pragma warning disable 1591 //ignore no documentation comment for this
        protected override void Draw()
        {
            if (_texture != null)
                _texture.Draw(Graphicslast);
        }

#pragma warning restore 1591

#pragma warning disable 1591
        public override void Dispose()
        {
            Ctrl.SizeChanged -= Ctrl_SizeChanged;
            base.OpacityChanged -= PictureBoxOverlay_OpacityChanged;
            base.LocationChanged -= PictureBoxOverlay_LocationChanged;
            base.Dispose();
            if (_texture != null)
                _texture.Dispose();
        }
#pragma warning restore 1591

#region fields to route

        

        /// <summary>
        /// ForeColor serves to change picture color. Usefull ONLY if the picture is composed of tranparent and white pixels.
        /// See the demo code for an example.
        /// </summary>
        public override System.Drawing.Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
                if (_texture != null)
                    _texture.Color = value;
            }
        }

        /// <summary>
        /// Image to set, can be bmp,jpeg,png,gif
        /// </summary>
        public System.Drawing.Image Image
        {
            get{return Ctrl.Image;}
            set {
                Ctrl.Image = value;
                if (_texture == null)
                    BuildTexture(value);
                else
                    _texture.ImageSource = value;
            }
        }

        private void BuildTexture(System.Drawing.Image value)
        {
            _texture = new g_Texture(value);
            _texture.Opacity = base.Opacity;
            _texture.SizeToDisplay = Ctrl.Size;
            _texture.Color = base.ForeColor;
            _texture.Location = base.Location;
            _texture.LocationOffset = PointToScreen(new Point(0, 0));
        }

        /// <summary>
        /// AutoSize and Zoom values will be ignored
        /// </summary>
        public System.Windows.Forms.PictureBoxSizeMode SizeMode
        {
            get { return Ctrl.SizeMode; }
            set { Ctrl.SizeMode = value; }
        }
#endregion
        #region fields to hide

#pragma warning disable 1591 //ignore no documentation comment warning for this
        [Browsable(false), Bindable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override string Text
        {
#pragma warning restore 1591
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
        #endregion
    }
}
